﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.Database;
using Temp.Model;

namespace Temp.Controller
{
    class MessageTempController
    {   
        private SQLiteFactory sqlf;
        private List<Lexicon> lexicons;
        private SQLite.SQLiteConnection conn;
        private Random rand;
        private List<string> messageTemplateList;

        public MessageTempController() {
            sqlf = new SQLiteFactory();
            rand = new Random();
            messageTemplateList = new List<String>();
            lexicons = new List<Lexicon>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="forWhat">the type column</param>
        /// <returns></returns>
        public List<Lexicon> getMessageTemplateForObj(String forWhat)
        {
            lexicons.Clear();
            conn = sqlf.getConnection();
            lexicons = conn.Query<Lexicon>("Select * from MessageTemplate where type='"+forWhat+"'").ToList();
            conn.Close();
            return lexicons;
        }


        public List<string> getMessageTemplateForItems()
        {
            List<MessageTemplate> groupReferenceList = new List<MessageTemplate>();
            List<MessageTemplate> temp = new List<MessageTemplate>();
            int randNum;

            messageTemplateList.Clear();
            conn = sqlf.getConnection();
            groupReferenceList = conn.Query<MessageTemplate>("SELECT DISTINCT groupReference FROM MessageTemplate WHERE type = \"forItem\"");
            foreach (MessageTemplate group in groupReferenceList)
            {
                temp = conn.Query<MessageTemplate>("SELECT * FROM MessageTemplate WHERE groupReference = ?", group.groupReference);
                randNum = rand.Next(0, temp.Count);
                messageTemplateList.Add(temp[randNum].message.ToString());
            }
            return messageTemplateList;
        }

    }
}
